package com.site.wisdom_construction.controller.statisticsModule;

import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.service.qualityModule.QualityDangerStatisticsService;
import com.site.wisdom_construction.service.statisticsModule.StatisticsQualityService;
import com.site.wisdom_construction.statisticsResult.QualityByHt;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** @Author daiyifei @Date 2022/2/12 @Description */
@Api(tags = "质量模块统计数据接口")
@RestController
@RequestMapping("StatisticsQuality")
@CrossOrigin
public class StatisticsQualityController {
  @Autowired private QualityDangerStatisticsService qualityDangerStatisticsService;

  @Autowired private StatisticsQualityService statisticsQualityService;

  @ApiOperation(value = "质量隐患总数据", notes = "测试数据:")
  @PostMapping("/QualityByHt")
  @NoToken
  public OneDataResult QualityByHt(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    QualityByHt qualityByHt = new QualityByHt();
    // 质量隐患总数
    qualityByHt.setCountQualityAll(statisticsQualityService.countQualityAll(map));
    // 待审核质量隐患数
    qualityByHt.setCountQualityAllByStatusA(statisticsQualityService.countQualityAllByStatusA(map));
    // 无效质量隐患数
    qualityByHt.setCountQualityAllByStatusW(statisticsQualityService.countQualityAllByStatusW(map));
    // 待整改质量隐患数
    qualityByHt.setCountQualityAllByStatusD(statisticsQualityService.countQualityAllByStatusD(map));
    // 已整改质量隐患数
    qualityByHt.setCountQualityAllByStatusY(statisticsQualityService.countQualityAllByStatusY(map));

    Double CountQualityAll = Double.valueOf(statisticsQualityService.countQualityAll(map));
    Double CountQualityAllByStatusY =
        Double.valueOf(statisticsQualityService.countQualityAllByStatusY(map));
    Double d = (CountQualityAllByStatusY / CountQualityAll) * 100;
    String workerTrain = String.valueOf(d);
    // 整改率
    if ("NaN".equals(workerTrain) || "0.0".equals(workerTrain)) {
      qualityByHt.setRectificationRate("0");
    }else if ("100.0".equals(workerTrain)){
      qualityByHt.setRectificationRate("100");
    }else {
      if (workerTrain.length() == 3) {
        qualityByHt.setRectificationRate(workerTrain.substring(0, 3));
      } else if (workerTrain.length() == 4) {
        qualityByHt.setRectificationRate(workerTrain.substring(0, 4));
      } else {
        qualityByHt.setRectificationRate(workerTrain.substring(0, 5));
      }
    }
    oneDataResult.setData(qualityByHt);
    return oneDataResult;
  }

  @ApiOperation(value = "根据质量隐患类型分类统计数据", notes = "测试数据:")
  @PostMapping("/countByQualityTypeName")
  @NoToken
  public OneDataResult countByQualityTypeName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsQualityService.countByQualityTypeName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据劳务队伍分类统计数据", notes = "测试数据:")
  @PostMapping("/countBySubcontractorsName")
  @NoToken
  public OneDataResult countBySubcontractorsName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsQualityService.countBySubcontractorsName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据隐患区域分类统计数据", notes = "测试数据:")
  @PostMapping("/countByDistrictName")
  @NoToken
  public OneDataResult countByDistrictName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsQualityService.countByDistrictName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据班组分类统计数据", notes = "测试数据:")
  @PostMapping("/countByTeamName")
  @NoToken
  public OneDataResult countByTeamName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsQualityService.countByTeamName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据负责人分类统计数据", notes = "测试数据:")
  @PostMapping("/countByResponsiblePerson")
  @NoToken
  public OneDataResult countByResponsiblePerson(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsQualityService.countByResponsiblePerson(map));
    return oneDataResult;
  }

  @ApiOperation(value = "当月质量隐患情况分析", notes = "测试数据:")
  @PostMapping("/selectQualityDangerStatistics")
  @NoToken
  public OneDataResult selectQualityDangerStatistics(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(qualityDangerStatisticsService.selectQualityDangerStatistics(map));
    return oneDataResult;
  }
}
